const jwt = require('jsonwebtoken')
const express = require('express')

const checkToken = express.Router()

checkToken.all('*', (req, res, next) => {
  const url = req.url
  const token = req.headers.authorization
  //不需要验证token的路由
  const noToken = ['/login', '/getCode', '/resetPwd']
  if (noToken.indexOf(url) != -1) {
    next()
  } else {
    if (token) {
      jwt.verify(token, 'webshow-clinic', (err, data) => {
        if (!err) {
          next()
        } else {
          res.status(401).send({ error: 1, msg: 'token无效或过期' })
        }
      })
    } else {
      res.status(401).send({ error: 1, msg: 'token无效或过期' })
    }
  }
})
module.exports = checkToken
